﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IPTV.DBUtility;
using MySql.Data.MySqlClient;
using IPTV.module;

/// <summary>
///Activity 的摘要说明
/// </summary>
public class ActivityDao
{
    public ActivityDao()
	{
	}

    public List<CourseDto> getFreeCourse()
    {

        string sql = " select s.`Name`,cve.video_name,cve.small_url,t.`Name`,cve.video_length,cve.remark   "
                + " from me_p_recommend_config rc,cms_video_ext cve,teacher t,`subject`s "
                + " where rc.entity_id = cve.video_id and t.ID = cve.teacher_id and s.ID = cve.`subject` and "
                + " rc.categorykind = 'CK_TGSG_FREE' and rc.is_publish = 1 and cve.poststatus = 1 order "
                + " by rc.refreshtime desc,rc.displayorder asc";

        List<CourseDto> courseList = new List<CourseDto>();
        try
        {
            MySqlDataReader reader = MySQLHelper.ExecuteReader(MySQLHelper.CONNECTION_STRING, sql, null);

            while (reader.Read())
            {
                CourseDto courseDto = new CourseDto();
                courseDto.SubjectId = reader[0].ToString();
                courseDto.VideoName = reader[1].ToString();
                courseDto.SmallUrl = reader[2].ToString();
                courseDto.TeacherName = reader[3].ToString();
                courseDto.VideoLength = reader[4].ToString();
                courseDto.Remark = reader[5].ToString();
                courseList.Add(courseDto);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        return courseList;

    }

    public List<CourseDto> getPlayLog(String userID)
    {

        string sql = " select s.name,cve.video_name,v.createdtime,cve.small_url,t.name,cve.video_length,cve.remark   "
                + " from me_tgsg_visitrecord v,subject s,cms_video_ext cve,teacher t "
                + " where v.entityid = cve.video_id and v.subject = s.id and cve.teacher_id = t.id and v.idtype = 'video' "
                + " and v.userid = @uid ORDER BY v.createdtime DESC ";

        List<CourseDto> courseList = new List<CourseDto>();
        MySqlParameter[] parms = new MySqlParameter[] 
        { 
                    new MySqlParameter( "@uid" , MySqlDbType.VarChar, 50 )
        };

        parms[0].Value = userID;
        try
        {
            MySqlDataReader reader = MySQLHelper.ExecuteReader(MySQLHelper.CONNECTION_STRING, sql, parms);

            while (reader.Read())
            {
                CourseDto courseDto = new CourseDto();
                courseDto.SubjectName = reader[0].ToString();
                courseDto.VideoName = reader[1].ToString();
                courseDto.CreateTime = reader[2].ToString();
                courseDto.SmallUrl = reader[3].ToString();
                courseDto.TeacherName = reader[4].ToString();
                courseDto.VideoLength = reader[5].ToString();
                courseDto.Remark = reader[6].ToString();
                courseList.Add(courseDto);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        return courseList;

    }

    public List<CourseDto> getAD()
    {

        string sql = " select s.`Name`,cve.video_name,cve.small_url,t.`Name`,cve.video_length,cve.remark "
                + " from me_p_recommend_config rc,cms_video_ext cve,teacher t,`subject` s "
                + " where rc.entity_id = cve.video_id and t.ID = cve.teacher_id and s.ID = cve.`subject "
                + " and rc.categorykind = 'CK_TGSG_FREE' order by rc.refreshtime desc,rc.displayorder asc ";

        List<CourseDto> courseList = new List<CourseDto>();
        try
        {
            MySqlDataReader reader = MySQLHelper.ExecuteReader(MySQLHelper.CONNECTION_STRING, sql, null);

            while (reader.Read())
            {
                CourseDto course = new CourseDto();
                course.SubjectName = reader[0].ToString();
                course.VideoName = reader[1].ToString();
                course.SmallUrl = reader[2].ToString();
                course.TeacherName = reader[3].ToString();
                course.VideoLength = reader[4].ToString();
                course.Remark = reader[5].ToString();
                courseList.Add(course);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        return courseList;

    }

}